package com.eeepay.eeepay_v2.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.model.UserInfo;
import com.eeepay.eeepay_v2.util.ConfigPorperties;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.eeepay_v2.enc.EncRSA;
import com.eeepay.eeepay_v2.util.GrpcUtils;
import com.eeepay.eeepay_v2.util.SPUtils;
import com.eeepay.v2_library.ui.ABBaseActivity;
import com.eeepay.v2_library.util.ABRegUtil;
import com.eeepay.v2_library.view.TitleBar;

import cn.eeepay.api.grpc.nano.UpdatePwdApiServiceGrpc;
import cn.eeepay.api.grpc.nano.UpdatePwdProto;
import io.grpc.ManagedChannel;

/**
 * Created by zw on 2016/3/29 0029.
 * 修改密码  、 找回密码界面
 */
public class PwdActivity extends ABBaseActivity implements View.OnClickListener {

    private final int MODIFY_PWD = 0;
    private final int FIND_PWD = 1;

    private TitleBar title_bar;
    private LinearLayout layout_oldPwd; //  旧密码
    private EditText et_oldPwd, et_newPwd, et_pwdConfirm;
    private TextView tv_showOldPwd, tv_showNewPwd, tv_showPwdConfirm;
    private Button btn_confirm;

    private String oldPwd, newPwd, rePwd;
    private String intentFlag;//跳转标志

    private String phone;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_pwd;
    }

    @Override
    protected void initWidget() {
        title_bar = getViewById(R.id.title_bar);

        layout_oldPwd = getViewById(R.id.layout_old_pwd);

        et_oldPwd = getViewById(R.id.et_old_pwd);
        et_oldPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
        et_newPwd = getViewById(R.id.et_new_pwd);
        et_newPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
        et_pwdConfirm = getViewById(R.id.et_pwd_confirm);
        et_pwdConfirm.setTransformationMethod(PasswordTransformationMethod.getInstance());

        tv_showOldPwd = getViewById(R.id.tv_show_old_pwd);
        tv_showNewPwd = getViewById(R.id.tv_show_new_pwd);
        tv_showPwdConfirm = getViewById(R.id.tv_show_pwd_confirm);
        btn_confirm = getViewById(R.id.btn_confirm);

        if (bundle != null) {
            intentFlag = bundle.getString(Constant.INTENT_FLAG);
            if (Constant.MODIFY_PWD.equals(intentFlag)) {//修改密码
                title_bar.setTiteTextView("修改密码");
            } else if (Constant.FIND_PWD.equals(intentFlag)) {//找回密码
                layout_oldPwd.setVisibility(View.GONE);
                phone = bundle.getString("phone");
                title_bar.setTiteTextView("找回密码");
            }
        }
    }

    @Override
    protected void eventOnClick() {
        tv_showOldPwd.setOnClickListener(this);
        tv_showNewPwd.setOnClickListener(this);
        tv_showPwdConfirm.setOnClickListener(this);
        btn_confirm.setOnClickListener(this);

    }

    @Override
    protected void sendHttpRequest(int taskId) {
        showProgressDialog();
        GrpcUtils.getInstance().doGrpcTask(Constant.URL.URL, Constant.URL.PORT
                , taskId, new GrpcUtils.AsyncTaskListener() {
            @Override
            public Object doInBack(ManagedChannel channel, int taskId) {
                UpdatePwdApiServiceGrpc.UpdatePwdApiServiceBlockingStub stub = UpdatePwdApiServiceGrpc.newBlockingStub(channel);
                UpdatePwdProto.UpdatePwdRequest request = new UpdatePwdProto.UpdatePwdRequest();
                switch (taskId) {
                    case MODIFY_PWD:
                        UpdatePwdProto.UpdatePwdResponse response = null;
                        try {
                            request.userId = UserInfo.getUserInfo2SP().getUserNo();
                            request.oldPwd = EncRSA.EncPass(oldPwd);
                            request.newPwd = EncRSA.EncPass(newPwd);
                            request.teamId = ConfigPorperties.getInstance().getTeamId();
                            response = stub.updatePwdInfo(request);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        return response;
                    case FIND_PWD:
                        try {
                            request.phone = phone;
                            request.newPwd = EncRSA.EncPass(newPwd);
                            request.teamId = ConfigPorperties.getInstance().getTeamId();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        return stub.saveNewPwd(request);
                }
                return null;
            }

            @Override
            public void updateUI(Object result, int taskId) {
                dismissProgressDialog();
                if (result == null) {
                    showToast("操作失败");
                    return;
                }
                UpdatePwdProto.UpdatePwdResponse response = (UpdatePwdProto.UpdatePwdResponse) result;
                showToast(response.resMsg.msg);
                if (!response.resMsg.status) {
                    return;
                }
                if (Constant.FIND_PWD.equals(intentFlag)) {//找回密码
                    Intent intent = new Intent(mContext, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    SPUtils.removeHistory(Constant.LOGIN_PASS);//add by zhuangeqin 2017年4月18日11:01:05 移除密码重新；重新登录
                    SPUtils.removeHistory(Constant.LOGIN_PHONE);//add by zhuangeqin 2017年4月18日11:01:05 移除手机号重新；重新登录
                    Bundle bundle = new Bundle();
                    bundle.putString("phone", phone);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
                finish();
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_show_old_pwd://旧密码
                modifySelect(tv_showOldPwd, et_oldPwd);
                break;
            case R.id.tv_show_new_pwd://新密码
                modifySelect(tv_showNewPwd, et_newPwd);
                break;
            case R.id.tv_show_pwd_confirm://密码确认
                modifySelect(tv_showPwdConfirm, et_pwdConfirm);
                break;
            case R.id.btn_confirm:
                if (examInput()) {
                    if (Constant.MODIFY_PWD.equals(intentFlag)) {//修改密码
                        sendHttpRequest(MODIFY_PWD);
                    } else if (Constant.FIND_PWD.equals(intentFlag)) {//找回密码
                        sendHttpRequest(FIND_PWD);
                    }
                }
                break;
        }
    }

    /**
     * 检验输入是否合法
     *
     * @return
     */
    private boolean examInput() {

        newPwd = et_newPwd.getText().toString().trim();
        rePwd = et_pwdConfirm.getText().toString().trim();

        if (TextUtils.isEmpty(newPwd)) {
            showToast("新密码不能为空");
            return false;
        }

        if (Constant.MODIFY_PWD.equals(intentFlag)) {//修改密码才有旧密码
            oldPwd = et_oldPwd.getText().toString().trim();
            if (TextUtils.isEmpty(newPwd)) {
                showToast("旧密码不能为空");
                return false;
            }
//            if (!ABRegUtil.isRegiest(oldPwd, ABRegUtil.REG_EPASS)) {
//                showToast("旧密码不合法,必须4-20位并含有数字和字母");
//                return false;
//            }
        }

        if (!ABRegUtil.isRegiest(newPwd, ABRegUtil.REG_EPASS)) {
            showToast("新密码不合法,必须4-20位并含有数字和字母");
            return false;
        }
        if (!newPwd.equals(rePwd)) {
            showToast("两次输入的密码不一致");
            return false;
        }
        return true;
    }

    private void modifySelect(TextView tv, EditText et) {
        if (tv.isSelected()) {
            //隐藏密码
            tv.setSelected(false);
            et.setTransformationMethod(PasswordTransformationMethod.getInstance());
        } else {
            //显示密码
            tv.setSelected(true);
            et.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        }
    }
}
